//Edit by AiYan, 11839387
package user.view;

import java.util.Vector;
import user.model.AirVModel;
import user.model.UserModel;
import user.ucm.AirlineInformation;
import user.ucm.UCGetAirline;
import user.ucm.UCGetMoney;
import user.ucm.UCPaying;
import user.ucm.UCSearchLine;

public class MainView extends javax.swing.JFrame {

    private boolean connect_DB;
    private int money;
    private Vector v = new Vector();
    private UserModel um;

    /** Creates new form MainView */
    public MainView(UserModel um) {
        this.um = um;
        initComponents();
        UCGetMoney ucgm = new UCGetMoney(um);
        if ((money = ucgm.run()) != 1) {
            this.price_TextField.setText(String.valueOf(money));
            UCGetAirline ucga = new UCGetAirline();
            if ((v = ucga.run()) != null) {
                this.airplane_List.setListData(v);
            } else {
                this.note_Label.setText("No valuable data!");
            }
        } else {
            this.note_Label.setText("Can't connect database!");
        }
    }

    public AirVModel getAirVModel() {
        AirVModel avm = new AirVModel();
        avm.setAirName_TextField(airName_TextField);
        avm.setDestination_TextField(destination_TextField);
        avm.setPrice_TextField(price_TextField);
        avm.setSetOut_TextField(setOut_TextField);
        avm.setAirplane_List(airplane_List);
        avm.setNum_TextField(num_TextField);
        avm.setSearch_TextField(search_TextField);
        return avm;
    }

    public UserModel getUserModel(){
        return um;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        balance_Label = new javax.swing.JLabel();
        inject_Button = new javax.swing.JButton();
        inject_TextField = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        airplane_List = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        airName_Label = new javax.swing.JLabel();
        setOut_Label = new javax.swing.JLabel();
        airName_TextField = new javax.swing.JTextField();
        setOut_TextField = new javax.swing.JTextField();
        price_Label = new javax.swing.JLabel();
        price_TextField = new javax.swing.JTextField();
        jSeparator3 = new javax.swing.JSeparator();
        destination_Label = new javax.swing.JLabel();
        destination_TextField = new javax.swing.JTextField();
        note_Label = new javax.swing.JLabel();
        payMoney_Button = new javax.swing.JButton();
        num_Label = new javax.swing.JLabel();
        num_TextField = new javax.swing.JTextField();
        search_TextField = new javax.swing.JTextField();
        search_Button = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("OperateView");

        jSeparator1.setAlignmentY(0.0F);

        jLabel1.setFont(new java.awt.Font("Arial", 1, 18));
        jLabel1.setText("Welcome! ");

        balance_Label.setFont(new java.awt.Font("Arial", 1, 12));
        balance_Label.setText("Balance:");

        inject_Button.setFont(new java.awt.Font("Arial", 1, 12));
        inject_Button.setText("Injection");

        inject_TextField.setFont(new java.awt.Font("Arial", 1, 12));

        airplane_List.setFont(new java.awt.Font("Arial", 1, 12));
        airplane_List.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "CP3001 To England", "CP3002 To Ireland", "CP3003 To China", "CP3004 To Australia", "CP3005 To Amercan", "CP3006 To Canada", "CP3007 To India", "CP3008 To Holand", "CP3009 To France", "CP3010 To U.N." };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        airplane_List.setToolTipText(" ");
        airplane_List.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                airplane_ListMouseClicked(evt);
            }
        });
        airplane_List.addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                airplane_ListAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });
        jScrollPane1.setViewportView(airplane_List);

        jLabel2.setFont(new java.awt.Font("Arial", 1, 14));
        jLabel2.setText("Airplane");

        jSeparator2.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jSeparator2.setAlignmentY(0.0F);

        airName_Label.setFont(new java.awt.Font("宋体", 1, 14));
        airName_Label.setText("Airline Name:");

        setOut_Label.setFont(new java.awt.Font("Arial", 1, 12));
        setOut_Label.setText("Set out in:");

        airName_TextField.setEditable(false);
        airName_TextField.setFont(new java.awt.Font("Arial", 1, 12));
        airName_TextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        setOut_TextField.setEditable(false);
        setOut_TextField.setFont(new java.awt.Font("Arial", 1, 12));
        setOut_TextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        price_Label.setFont(new java.awt.Font("Arial", 1, 12));
        price_Label.setText("Price:");

        price_TextField.setEditable(false);
        price_TextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        destination_Label.setFont(new java.awt.Font("Arial", 1, 12));
        destination_Label.setText("Destination:");

        destination_TextField.setEditable(false);
        destination_TextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        payMoney_Button.setFont(new java.awt.Font("Arial", 1, 12));
        payMoney_Button.setText("Pay");
        payMoney_Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                payMoney_ButtonActionPerformed(evt);
            }
        });

        num_Label.setFont(new java.awt.Font("Arial", 1, 12));
        num_Label.setText("How many:");

        num_TextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        num_TextField.setText("1");

        search_Button.setFont(new java.awt.Font("Arial", 1, 12));
        search_Button.setText("Search");
        search_Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                search_ButtonActionPerformed(evt);
            }
        });

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(balance_Label, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(inject_TextField)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(inject_Button))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 328, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(247, 247, 247))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 565, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 311, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(price_Label, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(setOut_Label, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(airName_Label, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(destination_Label, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(price_TextField, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(setOut_TextField)
                                .addComponent(airName_TextField, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE))
                            .addComponent(destination_TextField, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(num_Label)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(num_TextField, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(payMoney_Button))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(search_TextField, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(search_Button)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(note_Label, javax.swing.GroupLayout.DEFAULT_SIZE, 565, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(balance_Label, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(inject_Button)
                    .addComponent(inject_TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 239, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(airName_Label)
                            .addComponent(airName_TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(setOut_Label, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(setOut_TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(destination_Label)
                            .addComponent(destination_TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(15, 15, 15)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(price_Label)
                            .addComponent(price_TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(search_TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(search_Button))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(payMoney_Button)
                            .addComponent(num_Label)
                            .addComponent(num_TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(18, 18, 18)
                .addComponent(note_Label, javax.swing.GroupLayout.DEFAULT_SIZE, 13, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void airplane_ListAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_airplane_ListAncestorAdded
    }//GEN-LAST:event_airplane_ListAncestorAdded

    private void airplane_ListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_airplane_ListMouseClicked
        AirlineInformation ai = new AirlineInformation(this);
        switch (ai.run()) {
            case 0:
                this.note_Label.setText("Select End!");
                break;
            case 1:
                this.note_Label.setText("Connection failed");
                break;
            case 2:
                this.note_Label.setText("SQL wrong!");
                break;
            default:
                this.note_Label.setText("No infomation!");
                break;
        }
    }//GEN-LAST:event_airplane_ListMouseClicked

    private void payMoney_ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_payMoney_ButtonActionPerformed
        UCPaying ucp = new UCPaying(this);
        ucp.run();
    }//GEN-LAST:event_payMoney_ButtonActionPerformed

    private void search_ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_search_ButtonActionPerformed
        UCSearchLine ucsl = new UCSearchLine(this);
        ucsl.run();
    }//GEN-LAST:event_search_ButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel airName_Label;
    private javax.swing.JTextField airName_TextField;
    private javax.swing.JList airplane_List;
    private javax.swing.JLabel balance_Label;
    private javax.swing.JLabel destination_Label;
    private javax.swing.JTextField destination_TextField;
    private javax.swing.JButton inject_Button;
    private javax.swing.JTextField inject_TextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JLabel note_Label;
    private javax.swing.JLabel num_Label;
    private javax.swing.JTextField num_TextField;
    private javax.swing.JButton payMoney_Button;
    private javax.swing.JLabel price_Label;
    private javax.swing.JTextField price_TextField;
    private javax.swing.JButton search_Button;
    private javax.swing.JTextField search_TextField;
    private javax.swing.JLabel setOut_Label;
    private javax.swing.JTextField setOut_TextField;
    // End of variables declaration//GEN-END:variables
}
